<?php
/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-01 22:12:12;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */
namespace application\core;

class Request
{

    public static function getPagePars(): array
    {
        $data = self::json();

        return [
            'page' => $data['page'],
            'limit' => $data['limit'],
            'lup_sort_field' => $data['lup_sort_field'],
            'lup_sort_order' => $data['lup_sort_order']
        ];
    }

    public static function getHeader($key): string
    {
        $key = str_replace('_', '-', $key);
        $value_arr = self::getAllHeaders();
        return isset($value_arr[$key]) ? trim($value_arr[$key]) : '';
    }

    public static function getAllHeaders(): array
    {
        $headers = array();

        foreach ($_SERVER as $key => $value) {
            if (substr($key, 0, 5) === 'HTTP_') {
                $key = substr($key, 5);
                $key = str_replace('_', ' ', $key);
                $key = str_replace(' ', '-', $key);
                $key = strtolower($key);
                $headers[$key] = $value;
            }
        }
        return $headers;
    }

    public static function json($flg = true): array
    {
        $content = file_get_contents('php://input');
        $post = (array) json_decode($content, true);
        foreach ($post as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $kk => $vv) {
                    if($flg){
                        $post[$k][$kk] = Lib::xssFilter(trim($vv));
                    }else{
                        $post[$k][$kk] = trim($vv);
                    }
                }
            } else {
                if($flg) {
                    $post[$k] = Lib::xssFilter(trim($v));
                }else{
                    $post[$k] = trim($v);
                }
            }
        }
        return $post;
    }

    /**
     * POST接值处理
     */
    public static function post($key, $default = '', $type = '', $flg = true)
    {
        switch ($type) {
            case 'int':
                return isset($_POST[$key]) ? intval($_POST[$key]) : $default;
            case 'array':
                if (! empty($_POST[$key])) {
                    $returnData = [];
                    $data = array_filter($_POST[$key]);
                    foreach ($data as $v) {
                        if ($flg) {
                            $returnData[] = Lib::xssFilter($v);
                        } else {
                            $returnData[] = $v;
                        }
                    }
                    return implode(',', $returnData);
                } else {
                    return null;
                }
            default:
                if ($flg) {
                    return isset($_POST[$key]) ? Lib::xssFilter(trim($_POST[$key])) : Lib::xssFilter($default);
                } else {
                    return isset($_POST[$key]) ? trim($_POST[$key]) : $default;
                }
        }
    }

    /**
     * GET接值处理
     */
    public static function get($key, $default = '', $type = '', $flg = true)
    {
        switch ($type) {
            case 'int':
                return isset($_GET[$key]) ? intval($_GET[$key]) : $default;
            case 'array':
                if (! empty($_GET[$key])) {
                    $returnData = [];
                    $data = array_filter($_GET[$key]);
                    foreach ($data as $v) {
                        if ($flg) {
                            $returnData[] = Lib::xssFilter($v);
                        } else {
                            $returnData[] = $v;
                        }
                    }
                    return implode(',', $returnData);
                } else {
                    return null;
                }
            default:
                if ($flg) {
                    return isset($_GET[$key]) ? Lib::xssFilter(trim($_GET[$key])) : Lib::xssFilter($default);
                } else {
                    return isset($_GET[$key]) ? trim($_GET[$key]) : $default;
                }
        }
    }

    /**
     * REQUEST 接值处理
     */
    public static function request($key, $default = '', $type = '', $flg = true)
    {
        if ($type == 'int') {
            return isset($_REQUEST[$key]) ? intval($_REQUEST[$key]) : $default;
        } elseif ($type == 'array') {
            if (! empty($_REQUEST[$key])) {
                $returnData = [];
                $data = array_filter($_REQUEST[$key]);
                foreach ($data as $v) {
                    if ($flg) {
                        $returnData[] = Lib::xssFilter($v);
                    } else {
                        $returnData[] = $v;
                    }
                }
                return implode(',', $returnData);
            } else {
                return null;
            }
        } else {
            if ($flg) {
                return isset($_REQUEST[$key]) ? Lib::xssFilter(trim($_REQUEST[$key])) : Lib::xssFilter($default);
            } else {
                return isset($_REQUEST[$key]) ? trim($_REQUEST[$key]) : $default;
            }
        }
    }
}